package edu.sctu;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

public class Add extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req,
                         HttpServletResponse resp) throws ServletException, IOException {
        Add add = new Add();

        req.getRequestDispatcher("/addUser.jsp").forward(req, resp);
    }
    public void add(HttpServletRequest request,HttpServletResponse response) {

        String username = request.getParameter("username");
        String password = request.getParameter("password");


        Connection connection = null;
        Statement statement = null;


        String sql = "INSERT INTO t_users(username,password) " +
                "VALUES ('" + username + "','" + password + "')";

        try {
            connection = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone=UTC",
                    "root",
                    "123456"
            );

            statement = connection.createStatement();
            boolean resultSet = statement.execute(sql);


            connection.close();
            statement.close();


            response.sendRedirect("/userList");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

